<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>管理中心</title>
    <link
      rel="stylesheet"
      href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css"
      integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
      crossorigin="anonymous"
    />
    <link rel="stylesheet" href="./common.css" />
  </head>
  <body>
    <div class="container">
      <div class="row justify-content-md-center">
        <div class="col-lg-5 col-md-12">
          <h1 class="display-4">管理中心</h1>
          <p>
            <a class="btn btn-primary" href="/new.html">新建博客</a>
          </p>
          <div class="blog-list">
            <div class="form-group">
              <input
                type="text"
                class="form-control"
                id="text-keyword"
                placeholder="请输入关键字"
              />
            </div>
            <button
              type="button"
              class="btn btn-primary btn-block"
              id="btn-search"
            >
              搜索
            </button>
          </div>
					<div class="blog-list">
						<table id="table-container" class="table table-bordered">
							<tr>
								<th scope="col">博客标题</th>
								<th scope="col" colspan="2">操作</th>
							</tr>
						</table>
					</div>
        </div>
      </div>
    </div>

    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script>
      // 发送 get 请求
      function get(url) {
        return $.get(url);
      }

      // 发送 post 请求
      function post(url, data = {}) {
        return $.ajax({
          type: "post",
          url,
          data: JSON.stringify(data),
          contentType: "application/json"
        });
      }

      // 获取 url 参数
      function getUrlParams() {
        let paramStr = location.href.split("?")[1] || "";
        paramStr = paramStr.split("#")[0];
        const result = {};
        paramStr.split("&").forEach(itemStr => {
          const arr = itemStr.split("=");
          const key = arr[0];
          const val = arr[1];
          result[key] = val;
        });
        return result;
      }

      // 获取 dom 元素
      const $textKeyword = $("#text-keyword");
      const $btnSearch = $("#btn-search");
      const $tableContainer = $("#table-container");

      // 拼接接口 url
      let url = "/api/blog/list?isadmin=1"; // 增加一个 isadmin=1 参数，使用登录者的用户名，后端也需要修改 ！！！
      const urlParams = getUrlParams();
      if (urlParams.keyword) {
        url += "&keyword=" + urlParams.keyword;
      }

      // 加载数据
      get(url).then(res => {
        if (res.errno !== 0) {
          alert("数据错误");
          return;
        }

        // 显示数据
        const data = res.data || [];
        data.forEach(item => {
          $tableContainer.append(
            $(`
             <tr>
								<td>
									<a href="/detail.html?id=${item.id}" target="_blank">${item.title}</a>
								</td>
								<td>
									<a href="/edit.html?id=${item.id}">编辑</a>
								</td>
								<td>
									<a data-id="${item.id}" class="item-del">删除</a>
								</td>
							</tr>
            `)
          );
        });
      });

      // 搜索
      $btnSearch.click(e => {
        const keyword = $textKeyword.val();
        if (keyword) {
          location.href = "/admin.html?keyword=" + keyword;
        }
      });

      // 删除
      $tableContainer.click(e => {
        const $target = $(e.target);
        if ($target.hasClass("item-del") === false) {
          return;
        }

        if (confirm("确定删除？")) {
          const url = "/api/blog/del?id=" + $target.attr("data-id");
          post(url).then(res => {
            if (res.errno !== 0) {
              alert("操作错误");
              return;
            }
            location.href = location.href;
          });
        }
      });
    </script>
  </body>
</html>
